package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Controller
public class UserContorller {
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ModelAndView showregister(ModelAndView modelAndView){
        ModelAndView modelAndView1=new ModelAndView();
        modelAndView1.setViewName("register");
        modelAndView1.addObject("user",new User());
        return modelAndView;
    }
    @Autowired
    UserService userService;
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ModelAndView handleregister(ModelAndView mv,User user){
        try {
            userService.registerUser(user);
            mv.addObject("success","注册成功");
        }catch (Exception e){
            log.error(e.getMessage());
            mv.addObject("error","注册失败");
        }
        return mv;
    }
}
